package com.sg.service.base.component.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sg.common.util.CommonFunctionHelper;
import com.sg.dto.base.component.req.BatchQueryDataFolderReq;
import com.sg.dto.base.component.req.BatchQueryDocumentListByIdsReq;
import com.sg.dto.base.component.req.QueryDataFolderDetailReq;
import com.sg.dto.base.component.req.QueryDataFolderListReq;
import com.sg.entity.OmsUnstructuredDataFolder;
import com.sg.service.base.component.MOmsUnstructuredDataFolderService;
import com.wicket.okrcomponent.common.dto.FieldInfo;
import com.wicket.okrcomponent.common.exception.BizException;
import com.wicket.okrcomponent.dal.mapper.mbg.OmsUnstructuredDataFolderMapper;
import org.apache.skywalking.apm.toolkit.trace.Tag;
import org.apache.skywalking.apm.toolkit.trace.Tags;
import org.apache.skywalking.apm.toolkit.trace.Trace;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @author ：
 * @version 1.0
 * @since 2022/1/19 15:39
 */
@Service
public class MOmsUnstructuredDataFolderServiceImpl
        extends ServiceImpl<OmsUnstructuredDataFolderMapper, OmsUnstructuredDataFolder>
        implements MOmsUnstructuredDataFolderService {

    @Autowired
    OmsUnstructuredDataFolderMapper omsUnstructuredDataFolderMapper;

    /**
     * 2-7-02新增文件夹[2839]
     * gen by moon at 3/27/2024, 7:38:12 PM
     */
    @Trace(operationName = "2-7-02新增文件夹")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    @Override
    @Transactional(rollbackFor = Exception.class)
    public String addDataFolder(OmsUnstructuredDataFolder omsUnstructuredDataFolder) {
        omsUnstructuredDataFolder.setUnstructuredDataFolderId(CommonFunctionHelper.getUid());//DISTRIBUTED_ID
        omsUnstructuredDataFolder.setFileSize("0");//CUSTOM_CONVENTION
        omsUnstructuredDataFolder.setOrderNumber(Long.valueOf(0));//CUSTOM_CONVENTION
        omsUnstructuredDataFolder.setOperationInductionId(CommonFunctionHelper.getBaseInfoFromLocal().getSpecInductionRecordId());//CURRENT_INDUCTIONID
        omsUnstructuredDataFolder.setCreateInductionId(CommonFunctionHelper.getBaseInfoFromLocal().getSpecInductionRecordId());//CURRENT_INDUCTIONID
        omsUnstructuredDataFolder.setOperateTime(CommonFunctionHelper.getCurrentTime());//SYSTEM_CURRENT_TIME
        omsUnstructuredDataFolder.setCreateTime(CommonFunctionHelper.getCurrentTime());//SYSTEM_CURRENT_TIME
        omsUnstructuredDataFolder.setIsValid("FALSE");
        omsUnstructuredDataFolder.setAppId(CommonFunctionHelper.getBaseInfoFromLocal().getSpecAppId());//CURRENT_APPID
        if (super.save(omsUnstructuredDataFolder)) {
            return omsUnstructuredDataFolder.getUnstructuredDataFolderId();
        } else {
            return "-1";
        }

    }

    /**
     * 2-7-02查询文件夹详情[2842]
     * gen by moon at 3/27/2024, 12:58:48 AM
     */
    @Trace(operationName = "2-7-02查询文件夹详情")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    @Override
    @Transactional(rollbackFor = Exception.class)
    public OmsUnstructuredDataFolder queryDataFolderDetail(QueryDataFolderDetailReq reqDto) {

        try {
            reqDto.setIsValid("FALSE");
            QueryWrapper<OmsUnstructuredDataFolder> queryWrapper = new QueryWrapper<>();
            Map<String, Object> fieldInfoMap = null;
            fieldInfoMap = CommonFunctionHelper.objectToMapForLamBda(reqDto);
            CommonFunctionHelper.buildQueryWrapperEq(queryWrapper, fieldInfoMap, new ArrayList<>());
            queryWrapper.orderByDesc("create_time");
            List<OmsUnstructuredDataFolder> retList = super.list(queryWrapper);
            if (retList != null && retList.size() > 0) {
                OmsUnstructuredDataFolder retData = retList.get(0);

                return retData;
            }
            return null;
        } catch (Exception e) {
            e.printStackTrace();
            throw new BizException("-1", e, false);
        }
    }

    /**
     * 2-7-02删除文件夹[2909]
     * gen by moon at 9/6/2022, 10:39:45 PM
     */
    @Trace(operationName = "2-7-02删除文件夹")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean deleteDataFolder(String unstructuredDataFolderId) {

        return super.removeById(unstructuredDataFolderId);
    }

    /**
     * 2-7-02批量修改文件夹[3395]
     * gen by moon at 9/6/2022, 10:39:46 PM
     */
    @Trace(operationName = "2-7-02批量修改文件夹")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean batchUpdateDataFolder(List<OmsUnstructuredDataFolder> omsUnstructuredDataFolder) {

        if (omsUnstructuredDataFolder == null || omsUnstructuredDataFolder.size() == 0) {
            return false;
        }
        return super.saveOrUpdateBatch(omsUnstructuredDataFolder);

    }

    /**
     * 2-7-02批量查询文件夹[3397]
     * gen by moon at 9/6/2022, 10:39:46 PM
     */
    @Trace(operationName = "2-7-02批量查询文件夹")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    @Override
    @Transactional(rollbackFor = Exception.class)
    public List<OmsUnstructuredDataFolder> batchQueryDataFolder(BatchQueryDataFolderReq reqDto) {

        try {
            QueryWrapper<OmsUnstructuredDataFolder> queryWrapper = new QueryWrapper<>();

            if (reqDto.getDataFolderList() == null || reqDto.getDataFolderList().isEmpty()) {
                return new ArrayList<>();
            }
            queryWrapper.in("unstructured_data_folder_id", reqDto.getDataFolderList());
            Map<String, Object> fieldInfoMap = null;
            fieldInfoMap = CommonFunctionHelper.objectToMapForLamBda(reqDto);
            List<FieldInfo> compareList = CommonFunctionHelper.getRangeField(OmsUnstructuredDataFolder.class);
            CommonFunctionHelper.buildQueryWrapperCompare(queryWrapper, compareList, fieldInfoMap);
            List<FieldInfo> likeList = CommonFunctionHelper.getLikeFiled(OmsUnstructuredDataFolder.class);
            CommonFunctionHelper.buildQueryWrapperLike(queryWrapper, likeList, fieldInfoMap);
            likeList.addAll(compareList);
            CommonFunctionHelper.buildQueryWrapperEq(queryWrapper, fieldInfoMap, likeList);
            List<FieldInfo> orderList = CommonFunctionHelper.getOrderList(OmsUnstructuredDataFolder.class);
            //CommonFunctionHelper.buildQueryWrapperOrder(queryWrapper, orderList);

            return ((List<OmsUnstructuredDataFolder>) super.list(queryWrapper));
        } catch (Exception e) {
            e.printStackTrace();
            throw new BizException("-1", e, false);
        }
    }

    /**
     * 2-7-02查询文件夹列表[8799]
     * gen by moon at 3/27/2024, 7:38:17 PM
     */
    @Trace(operationName = "2-7-02查询文件夹列表")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    @Override
    @Transactional(rollbackFor = Exception.class)
    public List<OmsUnstructuredDataFolder> queryDataFolderList(QueryDataFolderListReq reqDto) {

        try {
            QueryWrapper<OmsUnstructuredDataFolder> queryWrapper = new QueryWrapper<>();
            reqDto.setIsValid("FALSE");

            Map<String, Object> fieldInfoMap = null;
            fieldInfoMap = CommonFunctionHelper.objectToMapForLamBda(reqDto);
            List<FieldInfo> compareList = CommonFunctionHelper.getRangeField(OmsUnstructuredDataFolder.class);
            CommonFunctionHelper.buildQueryWrapperCompare(queryWrapper, compareList, fieldInfoMap);
            List<FieldInfo> likeList = CommonFunctionHelper.getLikeFiled(OmsUnstructuredDataFolder.class);
            CommonFunctionHelper.buildQueryWrapperLike(queryWrapper, likeList, fieldInfoMap);
            likeList.addAll(compareList);
            CommonFunctionHelper.buildQueryWrapperEq(queryWrapper, fieldInfoMap, likeList);
            List<FieldInfo> orderList = CommonFunctionHelper.getOrderList(OmsUnstructuredDataFolder.class);
            //CommonFunctionHelper.buildQueryWrapperOrder(queryWrapper, orderList);
            queryWrapper.orderByAsc("order_number");
            queryWrapper.orderByDesc("create_time");
            queryWrapper.orderByDesc("operate_time");
            return ((List<OmsUnstructuredDataFolder>) super.list(queryWrapper));
        } catch (Exception e) {
            e.printStackTrace();
            throw new BizException("-1", e, false);
        }
    }

    /**
     * 2-7-01批量查询文档列表By文件夹Ids[8800]
     * gen by moon at 3/27/2024, 1:38:09 PM
     */
    @Trace(operationName = "2-7-01批量查询文档列表By文件夹Ids")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    @Override
    @Transactional(rollbackFor = Exception.class)
    public List<OmsUnstructuredDataFolder> batchQueryDocumentListByIds(BatchQueryDocumentListByIdsReq reqDto) {
        return null; //todo ruizhe dong
    }

    /**
     * 2-7-02修改文件夹[8804]
     * gen by moon at 3/27/2024, 7:38:18 PM
     */
    @Trace(operationName = "2-7-02修改文件夹")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean updateDataFolder(OmsUnstructuredDataFolder omsUnstructuredDataFolder) {

        return super.updateById(omsUnstructuredDataFolder);
    }

}

